package yjj.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import org.hibernate.validator.internal.constraintvalidators.bv.time.futureorpresent.FutureOrPresentValidatorForReadableInstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import yjj.model.security.LoginUser;
import yjj.model.web.Result;
import yjj.system.service.AuthenticationService;


@RestController
public class AuthenticationController {
    @Autowired
    AuthenticationService authenticationService;


    @PostMapping("/auth/login")
    @Operation(summary = "用户登录")
    public Result login(@RequestBody LoginUser user) {
        //要经过两层过滤器，第一个是验证token 第二个是验证用户密码
        LoginUser loginUser = authenticationService.login(user.getUsername());
        return Result.success();
    }
    @GetMapping("/auth/loginUser")
    @Operation(summary = "获取当前登录的用户信息")
    public Result getLoginUser() {
        LoginUser user = (LoginUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return Result.success(user);
    }
}
